﻿using System.Net.Http.Headers;
using System.Net.Http.Json;
using Newtonsoft.Json;

namespace LiveHub.DataModels;

public class UserHttp : IDisposable
{
    private string _jwt = "";

    private readonly HttpClient httpClient = new();

    public async Task<GroupModel?> GetGroup(string groupId)
    {
        try
        {
            var response = await httpClient.GetAsync($"{ModelData.ChatApiUrl}/api/Group/GetData/{groupId}");
            if (!response.IsSuccessStatusCode) return null;
            var s = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<GroupModel>(s);
        }
        catch
        {
            return null;
        }
    }

    public async Task<List<GroupModel>> GetUserGroups()
    {
        try
        {
            var response = await httpClient.GetAsync($"{ModelData.ChatApiUrl}/api/Group/GetGroups");
            if (response.IsSuccessStatusCode)
                return await response.Content.ReadFromJsonAsync<List<GroupModel>>() ?? [];

            return [];
        }
        catch
        {
            return [];
        }
    }

    public string Jwt
    {
        get => _jwt;
        set
        {
            _jwt = value;
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _jwt);
        }
    }

    public async Task<string> Login(LoginModel loginModel)
    {
        try
        {
            var response = await httpClient.PostAsJsonAsync($"{ModelData.ChatApiUrl}/api/User", loginModel);
            if (!response.IsSuccessStatusCode) return "";
            Jwt = await response.Content.ReadAsStringAsync();
            return Jwt;
        }
        catch
        {
            return "";
        }
    }

    public async Task<UserModel?> GetData()
    {
        try
        {
            var response = await httpClient.GetAsync($"{ModelData.ChatApiUrl}/api/User");

            if (!response.IsSuccessStatusCode) return null;

            var json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<UserModel>(json);
        }
        catch
        {
            return null;
        }
    }

    public async Task<GroupModel?> CreateOrJoinGroup(GroupModel model)
    {
        try
        {
            var response =
                await httpClient.PostAsJsonAsync($"{ModelData.ChatApiUrl}/api/Group/CreateOrJoinGroup", model);

            if (!response.IsSuccessStatusCode) return null;

            var json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<GroupModel>(json);
        }
        catch
        {
            return null;
        }
    }

    public async Task<GroupModel?> GetGroupFromId(string id)
    {
        try
        {
            var response =
                await httpClient.GetAsync($"{ModelData.ChatApiUrl}/api/Group/GetGroupFromId/{id}");

            if (!response.IsSuccessStatusCode) return null;

            var json = await response.Content.ReadAsStringAsync();
            return JsonConvert.DeserializeObject<GroupModel>(json);
        }
        catch
        {
            return null;
        }
    }

    public async Task<bool> QuitGroup(GroupModel model)
    {
        try
        {
            var response =
                await httpClient.PostAsJsonAsync($"{ModelData.ChatApiUrl}/api/Group/QuitGroup", model);

            return response.IsSuccessStatusCode;
        }
        catch
        {
            return false;
        }
    }

    public void Dispose()
    {
        httpClient.Dispose();
        GC.SuppressFinalize(this);
    }
}